<template>
	<view class="container">
		<view class="example">
			<uni-forms ref="form" :model="formData" labelWidth="80px">
				<uni-forms-item label="内部码" name="internalCode" class="item-margin">
					<uni-easyinput v-model="formData.internalCode" :disabled="isDisabled" placeholder="内部码" ref="start"
						@confirm="focusInput" />
				</uni-forms-item>
				<uni-forms-item label="成品码" name="id">
					<uni-easyinput v-model="formData.id" placeholder="成品码" ref="nextFocus" @confirm="submitForm" />
				</uni-forms-item>
			</uni-forms>
			<view class="uni-form-item word-wrap margin-15">
				<view class="" name="bz_internalCode">扫描数据1：{{formData.bz_internalCode}}</view>
			</view>
			<view class="uni-form-item word-wrap">
				<view class="" name="bz_id">扫描数据2：{{formData.bz_id}}</view>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		updateBoxscan
	} from "@/api/longyi/boxScanning/Index.js"

	export default {
		data() {
			return {
				formData: {
					id: '',
					internalCode: '',
					bz_id: '',
					bz_internalCode: ''
				},
				boxScanMsg: '',
				loading: false,
				inpFocus: true,
				isDisabled: false,
				rules: {
					internalCode: {
						rules: [{
							required: true,
							errorMessage: '内部码不能为空'
						}]
					}
				}
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
			this.focusInput1()
		},
		methods: {
			changeInputDisabled(val) {
				this.isDisabled = val;
				this.inpFocus = true;
			},
			submitForm() {
				//	console.log(this.formData)
				updateBoxscan(this.formData).then(response => {
					this.boxScanMsg = response.data
					//	console.log(response)
					this.formData.bz_id = this.formData.id
					//	this.bz_internalCode=this.formData.internalCode
					this.formData.id = ''
					this.formData.internalCode = ''
					this.$modal.msgSuccess("成品绑定内码成功！")
					this.focusInput1()

				}).catch(error => {
					// 处理错误的逻辑,重新输入
					this.changeInputDisabled(true);
					let that = this;
					uni.showModal({
						title: '错误信息',
						content: error,
						showCancel: false,
						confirmText: '确定', // 确定按钮的文本
						success: function(res) {
							if (res.confirm) {
								that.changeInputDisabled(false);
							}
						}
					});
					this.formData.bz = "提交失败，请重试！"
				});
			},
			focusInput() {
				this.formData.bz_internalCode = this.formData.internalCode
				this.$nextTick(() => {
					this.$refs.nextFocus.focused = true
					this.$refs.start.focused = false

				})
			},

			focusInput1() {
				this.$nextTick(() => {
					this.$refs.start.focused = true
					this.$refs.nextFocus.focused = false
				})
			},
		}
	}
</script>

<style lang="scss">
	.example {
		padding: 15px;
		background-color: #fff;
		margin: 10px auto;

		.item-margin.is-direction-left {
			margin-bottom: 15px !important;
		}

		.margin0.is-direction-left {
			margin-bottom: 0 !important;
		}
	}

	.footer-btn {
		padding: 15px;
		background-color: #fff;
		margin-top: 66px;
		position: fixed;
		width: 100%;
		bottom: 0;
	}

	.container {
		overflow: hidden;
	}

	.margin-15 {
		margin-bottom: 15px !important;
	}
</style>